﻿using System;
using System.Data.SqlClient;
using System.Web;

namespace scmsByAspdNet.cotro.admin {

  public class getRole : IHttpHandler {

    //请求来自 Admin.js
    //无附加参数
    //功能：用户是否为管理员，若不是管理员则返回404页面
    public void ProcessRequest(HttpContext context) {
      //数据处理
      var cooki = context.Request.Cookies.Get("token");
      string token = cooki == null ? null : cooki.Value;
      if (token == null) { context.Response.StatusCode = 200; context.Response.Write("{\"code\":-1}"); return; }
      //数据库
      SqlConnection conn = new SqlConnection("Data Source=106.54.69.143;database=scms;uid=sa;pwd=Aspdotnet666");
      conn.Open();
      //声明返回的数据
      string ret = "";
      //创建SQL命令
      SqlCommand cmd = new SqlCommand("select rname from sc_roles where rid =  (select role from sc_indiv_info where token='" + token + "');", conn);
      //执行SQL命令
      cmd.ExecuteNonQuery();
      //获取SqlDataReader
      SqlDataReader reader = cmd.ExecuteReader();
      //判断是否存在数据
      reader.Read();
      ret = $"{{\"code\":0,\"role\":\"{reader[0].ToString()}\"}}";
      conn.Close();
      //返回数据
      context.Response.StatusCode = 200;
      context.Response.Write(ret);
    }

    public bool IsReusable {
      get {
        return false;
      }
    }
  }
}